#include<stdio.h>
#include<string.h>
#define INTMAX 0x7fffffff
#define INTMIN 0x80000000
int myAtoi(char* str) {
	if (str==0) return 0;
	int len=strlen(str); 
	if (len==0) return 0;
	int k=0;bool neg=0; 
	while (k<len && (str[k]==' '||str[k]=='\t')) k++;
	if (k==len) return 0;
	if (str[k]=='-'){
		neg=1;
		k++;
	}else if (str[k]=='+'){
		k++;
	}
	long long v=0; 
	long long max=INTMAX;max+=neg;
	while (k<len && str[k]>='0' && str[k]<='9'){
		
		v*=10;
		v+=str[k]-'0';
		if (v>max){	//check outOfBound
			v=max;
			break;
		}	
		k++; 
	} 
	while (k<len){
		if (str[k]==' ' || str[k]=='\t'){
			k++;
		}else{
			return 0;
		}
		
	} 
	if (neg) v=-v;
	int x=-1;
	int ans=v; 
    return ans; 
}
int main(){
	short a=-32767;
	int b[8];
	b[0]=a;
	b[1]=5;
	b[2]=-1;
	b[3]=0xfffffffa;
	long long c=1;
	c=(c<<31);
	b[4]=c;
	b[5]=-2147483647;
	b[6]=-2147483648;
	b[7]=-2147483649;
	printf("%d\n",myAtoi("-50"));
	printf("%d\n",myAtoi("-50"));
	printf("%d\n",myAtoi(NULL));
	printf("%d\n",myAtoi(""));
	printf("%d\n",myAtoi("1"));
	printf("%d\n",myAtoi("+"));
	printf("%d\n",myAtoi("-"));

	printf("%d\n",myAtoi("+2323"));
	//printf("%s\n",myAtoi("-.43e2"));
	//printf("%s\n",myAtoi("1.23e-4"));
	//printf("%s\n",myAtoi("+043.4"));
	printf("%d\n",myAtoi("2147483647"));
	printf("%d\n",myAtoi("2147483648"));
	printf("%d\n",myAtoi("2147483649"));
	printf("%d\n",myAtoi("  -2147483647  "));
	printf("%d\n",myAtoi("  -2147483648  "));
	printf("%d\n",myAtoi("  -2147483649  "));
	printf("%d\n",myAtoi("\t  390002 "));
	printf("%d\n",myAtoi("   390002 "));
	printf("%d\n",myAtoi("  d -39002 "));
	printf("%d\n",myAtoi("   -39002 "));
	printf("%d\n",myAtoi("   +03024 "));
	printf("%d\n",myAtoi("2390320 289"));
	return 0;
} 